﻿using System;
using ControleRepresentante.Common;
using ControleRepresentante.Data.Catalogs;
using ControleRepresentante.Data.Helpers;

namespace ControleRepresentante.Data.CatalogSteps
{
    internal class CreateSystemUserCatalogStep : CatalogStepBase<ControleRepresentanteCatalogStepTypes>
    {
        protected override ControleRepresentanteCatalogStepTypes CatalogStepType
        {
            get { return ControleRepresentanteCatalogStepTypes.CreateSystemUser; }
        }

        public override void Execute(IDataAccess dataAccess)
        {
            new CatalogStepHelper(dataAccess)
                .CreateTable("SystemUser")
                .AddColumn("RepresentanteId", ColumnType.Guid, true, false)
                .AddForeignKey("RepresentanteId", "Representante")
                .AddColumn("Ativo", ColumnType.Boolean, false, false)
                .AddColumn("Username", ColumnType.ShortString, false, true)
                .AddColumn("Password", ColumnType.LongString, false, false)
                .AddColumn("IsAdmin", ColumnType.Boolean, false, false)
                .AddColumn("Email", ColumnType.ShortString, true, false);

            dataAccess.ExecuteCommand(@"
                INSERT INTO SystemUser (Id, Username, Password, IsAdmin, Ativo)
                VALUES (@Id, @Username, @Password, @IsAdmin, @Ativo)",
                new Parameter("Id", ParameterType.Guid, Guid.NewGuid()),
                new Parameter("Username", ParameterType.String, "admin"),
                new Parameter("Password", ParameterType.String, "admin".Encrypt()),
                new Parameter("IsAdmin", ParameterType.Boolean, true),
                new Parameter("Ativo", ParameterType.Boolean, true));
        }
    }
}